package Presentacion.contabilidad;

import clases.incio.ButtonRenderer;
import clases.incio.ButtonEditor;
import global.Dialogos;
import global.Iconos;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public final class dlgAsiento extends javax.swing.JDialog {
private Iconos ico = new Iconos();
    public static frmConsultaAsiento asiento;
    public static frmLibroDiario diario;
    public static dlgConsultaCuenta consulta;
    private DefaultTableModel modelo;
        private String[] fila;
    private float sumdebe, sumhaber;

    /**
     *
     * @param parent
     * @param modal
     */
    /*public dlgAsiento(frmLibroDiario parent, boolean modal) {
        diario = parent;
        setModal(modal);
        initComponents();
        setLocationRelativeTo(this);
        setResizable(false);
        iconos();
        setTitle("REGISTRO DE ASIENTO");
        iniciar();
        txtnumero.setText(Integer.toString(diario.objDiario.numero()));
        visiblidad(true);
        sumdebe = 0;
        sumhaber = 0;
    }*/

    public dlgAsiento(java.awt.Frame parent, boolean modal) {
        super(parent,modal);
        initComponents();
        setLocationRelativeTo(this);
        setResizable(false);
        iconos();
        iniciar();
        txtnumero.setText(Integer.toString(frmConsultaAsiento.objAsiento.numero()));
        visiblidad(true);
        sumdebe = 0;
        sumhaber = 0;
    }
    
    private void iconos() {
        btnnuevo.setIcon(ico.getNuevo());
        btnguardar.setIcon(ico.getGuardar());
        btnsalir.setIcon(ico.getSalir());
        btnactualizar.setIcon(ico.getActualizar());
    }
    
    
    public void llenardatosAsiento() {
        txtnumero.setText(Integer.toString(frmConsultaAsiento.objAsiento.getNumero()));
        txtdocumento.setText(frmConsultaAsiento.objAsiento.getDocumento());
        cbotipo.setSelectedItem(frmConsultaAsiento.objAsiento.getTipo());
        txtdescripcion.setText(frmConsultaAsiento.objAsiento.getDescripcion());
        dtfecha.setDate(global.Util.StringaDate(frmConsultaAsiento.objAsiento.getFecha()));
        armarmodelo();
        frmConsultaAsiento.objAsiento.llenardatosDetalleAsiento(modelo, tblasiento);
    }

    public void llenardatosAsientoDiario() {
        txtnumero.setText(Integer.toString(diario.objDiario.getNumero()));
        txtdocumento.setText(diario.objDiario.getDocumento());
        cbotipo.setSelectedItem(diario.objDiario.getTipo());
        txtdescripcion.setText(diario.objDiario.getDescripcion());
        dtfecha.setDate(global.Util.StringaDate(diario.objDiario.getFecha()));
        armarmodelo();
        diario.objDiario.llenardatosDetalleAsiento(modelo, tblasiento);
    }

    private void mostrarConsulta() {
        consulta = new dlgConsultaCuenta(this, true);
        consulta.setVisible(true);

    }

    public void limpiar() {
        txtcuenta.setText("");
        txtcodigo.setText("");
        txtdebe.setText("00.00");
        txthaber.setText("00.00");

    }

    private void armarmodelo() {
        modelo = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int fila, int columna) {
                return columna == 4;
            }
        };
        tblasiento.removeAll();
        tblasiento.setModel(modelo);
        modelo.addColumn("CODIGO");
        modelo.addColumn("CUNTA");
        modelo.addColumn("DEBE");
        modelo.addColumn("HABER");
        modelo.addColumn("QUITAR");
        DefaultTableCellRenderer centrar = new DefaultTableCellRenderer();
        centrar.setHorizontalAlignment(SwingConstants.RIGHT);
        tblasiento.getColumnModel().getColumn(2).setCellRenderer(centrar);
        tblasiento.getColumnModel().getColumn(3).setCellRenderer(centrar);

        TableColumn columna = new TableColumn();

        columna = tblasiento.getColumnModel().getColumn(1);
        columna.setPreferredWidth(300);
        columna = tblasiento.getColumnModel().getColumn(4);
        columna.setPreferredWidth(10);
    }

    private void agregar() {
        if (!global.Validaciones.comprobarNulo(txtcuenta)) {
            global.Mensajes.mostrarMensaje("Advertencia!", "Debe selecionar la cuenta", 2);
        } else if ("".equals(txtdebe.getText())) {
            global.Dialogos.Error("Ingrese el debe");
            txtdebe.requestFocus();
        } else if ("".equals(txthaber.getText())) {
            global.Dialogos.Error("Ingrese el haber");
            txthaber.requestFocus();
        } else {
            fila = new String[5];
            fila[0] = txtcodigo.getText();
            fila[1] = txtcuenta.getText();
            fila[2] = txtdebe.getText();
            fila[3] = txthaber.getText();
            tblasiento.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
            tblasiento.getColumnModel().getColumn(4).setCellEditor(
                    new ButtonEditor(new JCheckBox(), modelo, tblasiento));

            modelo.addRow(fila);
        }
    }

    private void iniciar() {
        txtdebe.setHorizontalAlignment(SwingConstants.RIGHT);
        txthaber.setHorizontalAlignment(SwingConstants.RIGHT);
        txtnumero.setEditable(false);
        dtfecha.setDate(global.Util.StringaDate(global.Util.getFechaActual()));
        cbotipo.setSelectedIndex(0);
        txtdocumento.setText("");
        txtdescripcion.setText("");
        txtcodigo.setEditable(false);
        txtcuenta.setEditable(false);
        cbotipo.requestFocus();
        limpiar();
        armarmodelo();

    }

    /*private boolean verificarCuenta(){
     for (int i=0; i < modelo.getRowCount(); i++){
     if(tblasiento.getValueAt(i, 0).equals(txtcodigo.getText())){
     return true ;
     }
      
     }return false;
     }*/
    public void visiblidad(boolean visibilidad) {
        if (visibilidad != true) {
            btnactualizar.setVisible(true);
            btnguardar.setVisible(false);
        } else {
            btnactualizar.setVisible(false);
            btnguardar.setVisible(true);
        }
    }

    private boolean Validacion() {
        if ("--Seleccione--".equals(cbotipo.getSelectedItem().toString())) {
            Dialogos.FaltaCampo("Tipo Asiento");
            cbotipo.requestFocus();
            return false;
        } else if ("".equals(txtdescripcion.getText())) {
            Dialogos.FaltaCampo("Descripcion");
            txtdescripcion.requestFocus();
            return false;
        } else if (tblasiento.getRowCount() == 0) {
            global.Mensajes.mostrarMensaje("Advertencia!", "No ha ingresado ninguna cuenta en el Asiento", 2);
            return false;
        } else if (tblasiento.getRowCount() == 1) {
            global.Mensajes.mostrarMensaje("Advertencia!", "El Asiento debe tener mas de una cuenta...", 2);
            return false;
        } else if (sumdebe != sumhaber) {
            global.Mensajes.mostrarMensaje("Advertencia!", "El Asiento no esta cuadrado...!", 2);
            return false;
        } else {
            return true;
        }
    }

    private void llenarTablaAsiento(int fila) {
        if (tblasiento.getRowCount() != 0) {
            txtcodigo.setText(tblasiento.getValueAt(fila, 0).toString());
            txtcuenta.setText(tblasiento.getValueAt(fila, 1).toString());
            txtdebe.setText(tblasiento.getValueAt(fila, 2).toString());
            txthaber.setText(tblasiento.getValueAt(fila, 3).toString());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar4 = new javax.swing.JToolBar();
        btnnuevo = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnactualizar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        txtnumero = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        dtfecha = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        cbotipo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txtdocumento = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        txtdescripcion = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblasiento = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        txtcodigo = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtcuenta = new javax.swing.JTextField();
        txtdebe = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txthaber = new javax.swing.JTextField();
        btnagregar = new javax.swing.JButton();
        btnbuscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jToolBar4.setBorder(null);
        jToolBar4.setFloatable(false);
        jToolBar4.setBorderPainted(false);

        btnnuevo.setText("Nuevo");
        btnnuevo.setFocusable(false);
        btnnuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnnuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });
        jToolBar4.add(btnnuevo);

        btnguardar.setText("Guardar");
        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnguardar);

        btnactualizar.setText("Actualizar");
        btnactualizar.setFocusable(false);
        btnactualizar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnactualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnactualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnactualizar);

        btnsalir.setText("Salir");
        btnsalir.setFocusable(false);
        btnsalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnsalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jToolBar4.add(btnsalir);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("NÚMERO");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("FECHA");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("TIPO ASIENTO");

        cbotipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "APERTURA", "AJUSTE", "CIERRE" }));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("DOCUMENTO");

        txtdocumento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtdocumentoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdocumentoKeyTyped(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel13.setText("DESCRIPCIÓN");

        txtdescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtdescripcionKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdescripcionKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtnumero, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dtfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 225, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbotipo, 0, 125, Short.MAX_VALUE)
                            .addComponent(txtdocumento))
                        .addGap(85, 85, 85))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txtdescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtnumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbotipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dtfecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(txtdocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(25, 25, 25)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtdescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblasiento.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "null"
            }
        ));
        tblasiento.setColumnSelectionAllowed(true);
        tblasiento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblasientoMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblasiento);
        tblasiento.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel11.setText("CÓDIGO");

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel12.setText("CUENTA");

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel14.setText("DEBE");

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel15.setText("HABER");

        btnagregar.setText("+");
        btnagregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnagregarActionPerformed(evt);
            }
        });

        btnbuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/buscar.png"))); // NOI18N
        btnbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(txtcuenta, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtdebe, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel14))
                        .addGap(37, 37, 37)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel15)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txthaber, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                                .addComponent(btnagregar)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(jLabel15))
                        .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtcodigo)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txthaber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtdebe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtcuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(btnbuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addComponent(btnagregar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 431, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        iniciar();
        visiblidad(true);
    }//GEN-LAST:event_btnnuevoActionPerformed

    private void guardarAsiento() {
        if (Validacion() == true) {
            frmConsultaAsiento.objAsiento.setDocumento(txtdocumento.getText());
            frmConsultaAsiento.objAsiento.setFecha(global.Util.DateaString(dtfecha.getDate()));
            frmConsultaAsiento.objAsiento.setTipo(cbotipo.getSelectedItem().toString());
            frmConsultaAsiento.objAsiento.setDescripcion(txtdescripcion.getText());
            if (frmConsultaAsiento.objAsiento.guardarAsiento() == true) {
                for (int i = 0; i < modelo.getRowCount(); i++) {
                    frmConsultaAsiento.objAsiento.setCodigocuenta(tblasiento.getValueAt(i, 0).toString());
                    frmConsultaAsiento.objAsiento.setDebe(Float.parseFloat(tblasiento.getValueAt(i, 2).toString()));
                    frmConsultaAsiento.objAsiento.setHaber(Float.parseFloat(tblasiento.getValueAt(i, 3).toString()));
                    frmConsultaAsiento.objAsiento.GuardarDetalleAsiento();

                }
                global.Dialogos.GuardadoconExito();
                dispose();
                iniciar();
                frmConsultaAsiento.generartabla();
            }
        }
    }

    private void guardarDiario() {
        if (diario.objDiario.EjercicioActivo() == false) {
            global.Mensajes.mostrarMensaje("Ejercicio", "No hay un ejercicio activo", 2);
            dispose();
        } else if (cbotipo.getSelectedIndex() == 0) {
            global.Mensajes.mostrarMensaje("Asiento", "Debe seleccionar el tipo", 2);
        } else if (tblasiento.getRowCount() == 0) {
            global.Mensajes.mostrarMensaje("Asiento", "No ha ingresado cuenta(s) en el Asiento", 2);

        } else if (tblasiento.getRowCount() == 1) {
            global.Mensajes.mostrarMensaje("Asiento", "Un asiento tiene mas de una cuenta...", 2);
        } else if (sumdebe != sumhaber) {
            global.Mensajes.mostrarMensaje("Asiento", "El asiento no esta cuadrado...!", 2);
        } else {
            diario.objDiario.setDocumento(txtdocumento.getText());
            diario.objDiario.setFecha(global.Util.DateaString(dtfecha.getDate()));
            diario.objDiario.setTipo(cbotipo.getSelectedItem().toString());
            diario.objDiario.setDescripcion(txtdescripcion.getText());
            diario.objDiario.guardarAsiento();
            for (int i = 0; i < modelo.getRowCount(); i++) {
                diario.objDiario.setCodigocuenta(tblasiento.getValueAt(i, 0).toString());
                diario.objDiario.setDebe(Float.parseFloat(tblasiento.getValueAt(i, 2).toString()));
                diario.objDiario.setHaber(Float.parseFloat(tblasiento.getValueAt(i, 3).toString()));
                diario.objDiario.GuardarDetalleAsiento();

            }
            global.Mensajes.mostrarMensaje("Asiento", "Datos guardados correctamente...", 1);
            iniciar();
            diario.generartabla();

        }
    }

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        sumdebe = 0;
        sumhaber = 0;
        for (int i = 0; i < modelo.getRowCount(); i++) {
            sumdebe = sumdebe + Float.parseFloat(tblasiento.getValueAt(i, 2).toString());
            sumhaber = sumhaber + Float.parseFloat(tblasiento.getValueAt(i, 3).toString());
        }
       /* if (inicial.vta.ExistenciaPestana("Asiento")) {
            if ("Asiento".equals(inicial.vta.getPestanaActual())) {
                guardarAsiento();
                if (inicial.vta.ExistenciaPestana("Libro Diario")) {
                    diario.generartabla();
                }
            } else if ("Libro Diario".equals(inicial.vta.getPestanaActual())) {
                guardarDiario();
                if (inicial.vta.ExistenciaPestana("Asiento")) {
                    asiento.generartabla();
                }
            }
        }*/
    }//GEN-LAST:event_btnguardarActionPerformed

    private void actualizarAsiento() {
        frmConsultaAsiento.objAsiento.setDocumento(txtdocumento.getText());
        frmConsultaAsiento.objAsiento.setFecha(global.Util.DateaString(dtfecha.getDate()));
        frmConsultaAsiento.objAsiento.setTipo(cbotipo.getSelectedItem().toString());
        frmConsultaAsiento.objAsiento.setDescripcion(txtdescripcion.getText());
        if (frmConsultaAsiento.objAsiento.actualizarAsiento() == true) {
            frmConsultaAsiento.objAsiento.EliminarDetaleAsiento();
            for (int i = 0; i < modelo.getRowCount(); i++) {
                frmConsultaAsiento.objAsiento.setCodigocuenta(tblasiento.getValueAt(i, 0).toString());
                frmConsultaAsiento.objAsiento.setDebe(Float.parseFloat(tblasiento.getValueAt(i, 2).toString()));
                frmConsultaAsiento.objAsiento.setHaber(Float.parseFloat(tblasiento.getValueAt(i, 3).toString()));
                frmConsultaAsiento.objAsiento.GuardarDetalleAsiento();

            }
            global.Mensajes.mostrarMensaje("Correcto!", "Datos actualizados correctamente...", 1);
            iniciar();
            frmConsultaAsiento.generartabla();
            dispose();
        }
    }

    private void actualizarDiario() {
        diario.objDiario.setDocumento(txtdocumento.getText());
        diario.objDiario.setFecha(global.Util.DateaString(dtfecha.getDate()));
        diario.objDiario.setTipo(cbotipo.getSelectedItem().toString());
        diario.objDiario.setDescripcion(txtdescripcion.getText());
        diario.objDiario.actualizarAsiento();
        diario.objDiario.EliminarDetaleAsiento();
        for (int i = 0; i < modelo.getRowCount(); i++) {
            diario.objDiario.setCodigocuenta(tblasiento.getValueAt(i, 0).toString());
            diario.objDiario.setDebe(Float.parseFloat(tblasiento.getValueAt(i, 2).toString()));
            diario.objDiario.setHaber(Float.parseFloat(tblasiento.getValueAt(i, 3).toString()));
            diario.objDiario.GuardarDetalleAsiento();

        }
        global.Mensajes.mostrarMensaje("Asiento", "Datos actualizados correctamente...", 1);
        iniciar();
        diario.generartabla();
        dispose();
    }

    private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarActionPerformed
        sumdebe = 0;
        sumhaber = 0;
        for (int i = 0; i < modelo.getRowCount(); i++) {
            sumdebe = sumdebe + Float.parseFloat(tblasiento.getValueAt(i, 2).toString());
            sumhaber = sumhaber + Float.parseFloat(tblasiento.getValueAt(i, 3).toString());

        }
       /* if (Validacion() == true) {
            if ("Asiento".equals(inicial.vta.getPestanaActual())) {
                actualizarAsiento();
                if (inicial.vta.ExistenciaPestana("Libro Diario")) {
                    diario.generartabla();
                }
            } else if ("Libro Diario".equals(inicial.vta.getPestanaActual())) {
                actualizarDiario();
                if (inicial.vta.ExistenciaPestana("Asiento")) {
                    asiento.generartabla();
                }
            }
        }*/
    }//GEN-LAST:event_btnactualizarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        dispose();
        limpiar();
    }//GEN-LAST:event_btnsalirActionPerformed

    private void txtdocumentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdocumentoKeyReleased
        global.Validaciones.convertirMayuscula(txtdocumento);
    }//GEN-LAST:event_txtdocumentoKeyReleased

    private void txtdocumentoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdocumentoKeyTyped
        global.Validaciones.numerosLetras(evt, txtdocumento);
    }//GEN-LAST:event_txtdocumentoKeyTyped

    private void txtdescripcionKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdescripcionKeyReleased
        global.Validaciones.convertirMayuscula(txtdescripcion);
    }//GEN-LAST:event_txtdescripcionKeyReleased

    private void txtdescripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdescripcionKeyTyped
        global.Validaciones.numerosLetras(evt, txtdescripcion);
    }//GEN-LAST:event_txtdescripcionKeyTyped

    private void btnagregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnagregarActionPerformed
        agregar();
        limpiar();
    }//GEN-LAST:event_btnagregarActionPerformed

    private void btnbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbuscarActionPerformed
        mostrarConsulta();
        txtcuenta.setText(consulta.descripcion);
        txtcodigo.setText(consulta.id);
    }//GEN-LAST:event_btnbuscarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        iniciar();
    }//GEN-LAST:event_formWindowClosed

    private void tblasientoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblasientoMouseClicked
        if (evt.getClickCount() == 2) {
            llenarTablaAsiento(tblasiento.getSelectedRow());
            modelo.removeRow(tblasiento.getSelectedRow());
        }
    }//GEN-LAST:event_tblasientoMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgAsiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgAsiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgAsiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgAsiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dlgAsiento dialog = new dlgAsiento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                
                dialog.setVisible(true);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnactualizar;
    private javax.swing.JButton btnagregar;
    private javax.swing.JButton btnbuscar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton btnsalir;
    private javax.swing.JComboBox cbotipo;
    private com.toedter.calendar.JDateChooser dtfecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JTable tblasiento;
    private javax.swing.JTextField txtcodigo;
    private javax.swing.JTextField txtcuenta;
    private javax.swing.JTextField txtdebe;
    private javax.swing.JTextField txtdescripcion;
    private javax.swing.JTextField txtdocumento;
    private javax.swing.JTextField txthaber;
    private javax.swing.JTextField txtnumero;
    // End of variables declaration//GEN-END:variables
}
